package com.hjt.zhibo.collect.impl;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hjt.zhibo.collect.ITVPlatform;
import com.hjt.zhibo.model.PlatFormCatalog;
import com.hjt.zhibo.model.PlatFormRoom;
import com.hjt.zhibo.model.TVCatalog;
import com.hjt.zhibo.model.TVRoom;
import com.hjt.zhibo.tools.HttpContent;
import com.jfinal.plugin.activerecord.Db;

public class DouyuTV implements ITVPlatform{

	private String prefix = "douyu";
	
	private String baseUrl = "http://www.douyu.com";
	private String catalogUrl = "http://www.douyu.com/directory/";
	private String platform = "斗鱼";
	
	//private EncacheTools encacheTools;
	
	@Override
	public List<TVCatalog> getTVCatalog() {
		List<TVCatalog> list = new ArrayList<TVCatalog>();
		
		Document doc = Jsoup.parse(HttpContent.getUrlContent(catalogUrl));
		Elements links = doc.select("#live-list-contentbox li");
		List<Integer> platFormCatalogIds = new ArrayList<Integer>();
		
		for (Element link : links) {
			Element linkA = link.select("a").first();
			String url = linkA.attr("href");
			if(url.startsWith("http:")){
				continue;
			} else{
				String fullUrl = baseUrl+url;
				list.add(new TVCatalog(linkA.select("p.title").first().html(), fullUrl, ""));
				
				PlatFormCatalog tempPlatFormCatalog = PlatFormCatalog.dao.findFirst("select * from platform_catalog t where t.platform_name=? and t.platform_url=? ", platform, fullUrl);
				if(tempPlatFormCatalog != null){
					tempPlatFormCatalog.set("platform_name", platform)
					.set("platform_cn_name", linkA.select("p.title").first().html())
					.set("platform_en_name", "")
					.set("platform", platform)
					.set("prefix", prefix)
					.set("platform_url", fullUrl).update();
					platFormCatalogIds.add(tempPlatFormCatalog.getInt("platform_catalog_id"));
					continue;
				} else{
					PlatFormCatalog  _platFormCatalog = new PlatFormCatalog().set("platform_name", platform)
						.set("platform_cn_name", linkA.select("p.title").first().html())
						.set("platform_en_name", "")
						.set("platform", platform)
						.set("prefix", prefix)
						.set("platform_url", fullUrl);
					_platFormCatalog.save();
					platFormCatalogIds.add(_platFormCatalog.getInt("platform_catalog_id"));
				}
			}
		}
		
		StringBuilder sb =new StringBuilder(" delete from platform_catalog  where platform_name = ? and  platform_catalog_id not in ( ");
		for(Integer i=0, len=platFormCatalogIds.size(); i<len; i++){
			if(i > 0){
				sb.append(",");
			}
			sb.append(platFormCatalogIds.get(i));
		}
		sb.append(")");
		Db.update(sb.toString(), platform);
		return list;
	}

	@Override
	@Deprecated
	public List<TVRoom> getTVRoom(TVCatalog tVCatalog) {
		List<TVRoom> list = new ArrayList<TVRoom>();
		
		Document doc = Jsoup.parse(HttpContent.getUrlContent(tVCatalog.getUrl()));
		Elements links = doc.select("ul#live-list-contentbox li");
		for (Element link : links) {
			try {
				Element linkA = link.select("a").first();
				Element img = linkA.select("img").first();
				
				String imgSrc = img.attr("data-original");
				String title = linkA.attr("title");
				
				Element userInfoDiv = linkA.select(".mes").first();
				String auth = userInfoDiv.select("p span.dy-name.ellipsis.fl").first().html();
				String count = userInfoDiv.select("p span.dy-num.fr").first().html().trim();
				String roomType = userInfoDiv.select("div.mes-tit span").first().html();
try {
				System.out.println(count);
				if(count.endsWith("万")){
					System.out.println("判断.....");
					double _count = Double.valueOf(count.substring(0, count.length()-1)) * 10000;
					count = String.valueOf((long)_count);
					System.out.println("修改后的count: " + count);
					System.out.println("-------------");
				}
} catch (Exception e) {
	e.printStackTrace();
}
				
				String roomUrl = baseUrl + linkA.attr("href");
				TVRoom tvRoom = new TVRoom(imgSrc, title, auth, count, roomUrl, roomType, platform);
				list.add(tvRoom);
				//EncacheTools.getInstance().putCache("room", prefix+auth, tvRoom);
			} catch (Exception e) {
				continue;
			}
		}
		return list;
	}

	@Override
	public List<TVRoom> getTVRoom(PlatFormCatalog platFormCatalog) {
		List<TVRoom> list = new ArrayList<TVRoom>();
		
		Document doc = Jsoup.parse(HttpContent.getUrlContent(platFormCatalog.getStr("platform_url")));
		Elements links = doc.select("ul#live-list-contentbox li");
		
		List<Integer> ids = new ArrayList<Integer>();
		
		for (Element link : links) {
			try {
				Element linkA = link.select("a").first();
				Element img = linkA.select("img").first();
				
				String imgSrc = img.attr("data-original");
				String title = linkA.attr("title");
				
				Element userInfoDiv = linkA.select(".mes").first();
				String auth = userInfoDiv.select("p span.dy-name.ellipsis.fl").first().html();
				String count = userInfoDiv.select("p span.dy-num.fr").first().html().trim(), countTxt = count;
				if(count.endsWith("万")){
					double _count = Double.valueOf(count.substring(0, count.length()-1)) * 10000;
					count = String.valueOf((long)_count);
				}
				
				String roomType = userInfoDiv.select("div.mes-tit span").first().html();

				String roomUrl = baseUrl + linkA.attr("href");
				TVRoom tvRoom = new TVRoom(imgSrc, title, auth, count, roomUrl, roomType, platform);
				list.add(tvRoom);
				
				PlatFormRoom tempPlatFormRoom = PlatFormRoom.dao.findFirst("select * from platform_room t where t.auth=? and t.url=?", auth, roomUrl);
				
				if(tempPlatFormRoom != null){
					tempPlatFormRoom.set("platform_catalog_id", platFormCatalog.getInt("platform_catalog_id"))
					.set("room_name", title)
					.set("auth", auth)
					.set("count", count)
					.set("count_txt", countTxt)
					.set("url", roomUrl)
					.set("room_type", roomType)
					.set("platform", platform)
					.set("pic", imgSrc);
					
					tempPlatFormRoom.update();
					ids.add(tempPlatFormRoom.getInt("platform_room_id"));
				} else{
					PlatFormRoom platFormRoom = new PlatFormRoom()
					.set("platform_catalog_id", platFormCatalog.getInt("platFormCatalog"))
					.set("room_name", title)
					.set("auth", auth)
					.set("count", count)
					.set("count_txt", countTxt)
					.set("url", roomUrl)
					.set("room_type", roomType)
					.set("platform", platform)
					.set("pic", imgSrc);
					
					platFormRoom.save();
					ids.add(platFormRoom.getInt("platform_room_id"));
				}
				
			} catch (Exception e) {
				//e.printStackTrace();
				continue;
			}
		}
		
		if(ids.size() > 0) {
			StringBuilder sb =new StringBuilder(" delete from platform_room  where platform = ? and platform_catalog_id=? and  platform_room_id not in ( ");
			for(Integer i=0, len=ids.size(); i<len; i++){
				if(i > 0){
					sb.append(",");
				}
				sb.append(ids.get(i));
			}
			sb.append(" ) ");
			Db.update(sb.toString(), platform, platFormCatalog.getInt("platform_catalog_id"));
		} else{
			Db.update("delete from platform_room  where platform = ? and platform_catalog_id=? ", platform, platFormCatalog.getInt("platform_catalog_id"));
		}
		
		return list;
	}
	
}
